<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace api\home\controller;

use cmf\controller\RestBaseController;
use think\Validate;
use app\portal\model\PortalPostModel;
/**
 * @title 主要服务
 */
class ServiceController extends RestBaseController
{

    /**
     * @title 主要服务分类
     * @description 接口说明
     * @author 开发者
     * @url /api/home/service/serviceType
     * @method GET
     *
     * @param name:language type:string require:0 default: other desc:语言切换（英文传递此字段（en）,中文无需传递）
     *
     * @return data:服务分类@
     * @data id:服务分类id name:服务分类名称
     */
    public function serviceType(){
        $language = $this->request->param('language');

        if(isset($language) && !empty($language)){
            //英文
            $field_service = 'id,name_en name';
        }else{
            //中文
            $field_service = 'id,name';
        }
        $parent_res = CommonController::selectNoPositionData('service',$field_service);
        $this->success('成功',['data'=>$parent_res]);
    }

    /**
     * @title 主要服务详情
     * @description 接口说明
     * @author 开发者
     * @url /api/home/service/serviceDetail
     * @method GET
     *
     * @param name:language type:string require:0 default: other desc:语言切换（英文传递此字段（en）,中文无需传递）
     * @param name:service_id type:inter require:1 default: other desc:服务分类id
     *
     * @return id:服务分类id
     * @return name:服务分类名称
     * @return detail:详情（富文本）
     */
    public function serviceDetail(){
        $language = $this->request->param('language');
        $service_id = $this->request->param('service_id');
        //验证
        $rule = config('site.service_id');
        $validate = new Validate($rule['rule'],$rule['msg']);
        if (!$validate->check(['service_id'=>$service_id])) {
            $this->error($validate->getError());
        }
        if(isset($language) && !empty($language)){
            //英文
            $field_service = 'id,name_en name,detail_en detail';
        }else{
            //中文
            $field_service = 'id,name,detail';
        }
        $res = CommonController::findData('service',['id'=>$service_id],$field_service);
        if($res){
            $contentModel = new PortalPostModel();
            $res['detail'] = $contentModel->getPostContentAttr($res['detail']);
        }
        $this->success('成功',$res);
    }
}
